package controller;

import java.awt.GridLayout;
import java.awt.event.*;

import javax.swing.*;

import logic.*;

/**
 * this is the InitController class. 
 * this class can set several things in the simulation.
 * @author Vossen en Konijnen group 2
 *
 */
public class InitController extends AbstractController implements ActionListener 
{
	private JButton init;
	private JTextField height;
	private JTextField width;
	private JTextField stepSpeed;

	/**
	 * makes a new panel and sets the textfields and buttons in the correct order.
	 * @param simulator
	 */
	public InitController(Simulator simulator)
	{
		super(simulator);
		setSize(100, 130);
		setLayout(new GridLayout(20,1));
		JLabel label= new JLabel("Heigth");
		add(label);
		height = new JTextField();
		add(height);
		label= new JLabel("Width");
		add(label);
		width = new JTextField();
		add(width);
		label= new JLabel("Step speed");
		add(label);
		stepSpeed = new JTextField();
		add(stepSpeed);
		init = new JButton("Init");
		init.addActionListener(this);
		add(init);
		setVisible(true);
	}
	
	@Override
	public void actionPerformed(ActionEvent e) 
	{
		try
		{
			simulator.setField(parseWidth(),parseHeight());
			simulator.setStepSpeed(parseSpeed());
		}
		catch(Exception ex)
		{
			JOptionPane.showMessageDialog(null, "Dit zijn geen cijfers >_>");
		}
	}
	
	/**
	 * makes from the enterd width a number instead of text.
	 * @return int
	 * @throws NumberFormatException
	 */
	public int parseWidth() throws NumberFormatException
	{
		return Integer.parseInt(width.getText());
	}
	
	/**
	 * makes from the enterd height a number instead of text.
	 * @return int
	 * @throws NumberFormatException
	 */
	public int parseHeight()throws NumberFormatException
	{
		return Integer.parseInt(height.getText());
	}
	
	/**
	 * makes from the enterd speed a number instead of text.
	 * @return int
	 * @throws NumberFormatException
	 */
	public int parseSpeed()throws NumberFormatException
	{
		return Integer.parseInt(stepSpeed.getText());
	}
}
